#!/usr/bin/env python3
"""
面向 rubygem-sdoc 软件的 MCP Server
提供以下 MCP tools：
1. sdoc_merge_help - 测试 sdoc-merge 命令的帮助信息
   参数：无
   返回：执行结果 JSON
2. sdoc_help - 测试 sdoc 命令的基本帮助、版本和调试选项
   参数：无
   返回：执行结果 JSON
3. sdoc_ri_and_templates - 测试 sdoc 的 RI 格式生成和模板相关功能
   参数：无
   返回：执行结果 JSON
4. sdoc_output_options - 测试 sdoc 的输出目录和格式化选项
   参数：无
   返回：执行结果 JSON
5. sdoc_format_options - 测试 sdoc 的格式、包含目录和覆盖报告选项
   参数：无
   返回：执行结果 JSON
6. sdoc_advanced_options - 测试 sdoc 的高级选项如可见性、编码、扩展等
   参数：无
   返回：执行结果 JSON
7. sdoc_github - 测试 sdoc 的 GitHub 集成功能
   参数：无
   返回：执行结果 JSON

所有 tool 返回统一 JSON 结构：
{
  "success": bool,
  "command": str,
  "exit_code": int,
  "stdout": str,
  "stderr": str
}
"""

import subprocess
import os
from mcp.server.fastmcp import FastMCP, Context

mcp = FastMCP("Rubygem-sdoc MCP Server")

@mcp.tool()
def sdoc_merge_help(ctx: Context) -> dict:
    """
    测试 sdoc-merge 命令的帮助信息
    
    返回：
        dict: 包含执行结果的 JSON 对象
    """
    try:
        result = subprocess.run(
            ["sdoc-merge", "-h"],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": "sdoc-merge -h",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": "sdoc-merge -h",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def sdoc_help(ctx: Context) -> dict:
    """
    测试 sdoc 命令的基本帮助、版本和调试选项
    
    返回：
        dict: 包含执行结果的 JSON 对象
    """
    try:
        commands = [
            ["sdoc", "-h"],
            ["sdoc", "--help"],
            ["sdoc", "-v"],
            ["sdoc", "--version"],
            ["sdoc", "-V"],
            ["sdoc", "--verbose"],
            ["sdoc", "-q"],
            ["sdoc", "--quiet"],
            ["sdoc", "-D"],
            ["sdoc", "--debug"]
        ]
        
        all_results = []
        for cmd in commands:
            result = subprocess.run(
                cmd,
                capture_output=True,
                text=True,
                timeout=30
            )
            all_results.append({
                "success": result.returncode == 0,
                "command": " ".join(cmd),
                "exit_code": result.returncode,
                "stdout": result.stdout,
                "stderr": result.stderr
            })
        
        # 返回第一个命令的结果作为主要结果
        return all_results[0]
    except Exception as e:
        return {
            "success": False,
            "command": "sdoc help commands",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def sdoc_ri_and_templates(ctx: Context) -> dict:
    """
    测试 sdoc 的 RI 格式生成和模板相关功能
    
    返回：
        dict: 包含执行结果的 JSON 对象
    """
    try:
        commands = [
            ["sdoc", "-r"],
            ["sdoc", "--ri"],
            ["sdoc", "-R"],
            ["sdoc", "--ri-site"],
            ["sdoc", "-t", "testdemo"],
            ["sdoc", "--title", "testdemo"],
            ["sdoc", "-T", "rails"],
            ["sdoc", "--template", "rails"]
        ]
        
        all_results = []
        for cmd in commands:
            result = subprocess.run(
                cmd,
                capture_output=True,
                text=True,
                timeout=30
            )
            all_results.append({
                "success": result.returncode == 0,
                "command": " ".join(cmd),
                "exit_code": result.returncode,
                "stdout": result.stdout,
                "stderr": result.stderr
            })
        
        return all_results[0]
    except Exception as e:
        return {
            "success": False,
            "command": "sdoc ri and templates commands",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def sdoc_output_options(ctx: Context) -> dict:
    """
    测试 sdoc 的输出目录和格式化选项
    
    返回：
        dict: 包含执行结果的 JSON 对象
    """
    try:
        commands = [
            ["sdoc", "-o", "tmpdir1"],
            ["sdoc", "--output", "tmpdir2"],
            ["sdoc", "--op", "tmpd"],
            ["sdoc", "-N"],
            ["sdoc", "--line-numbers"],
            ["sdoc", "-A"],
            ["sdoc", "--hyperlink-all"]
        ]
        
        all_results = []
        for cmd in commands:
            result = subprocess.run(
                cmd,
                capture_output=True,
                text=True,
                timeout=30
            )
            all_results.append({
                "success": result.returncode == 0,
                "command": " ".join(cmd),
                "exit_code": result.returncode,
                "stdout": result.stdout,
                "stderr": result.stderr
            })
        
        return all_results[0]
    except Exception as e:
        return {
            "success": False,
            "command": "sdoc output options commands",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def sdoc_format_options(ctx: Context) -> dict:
    """
    测试 sdoc 的格式、包含目录和覆盖报告选项
    
    返回：
        dict: 包含执行结果的 JSON 对象
    """
    try:
        commands = [
            ["sdoc", "-C"],
            ["sdoc", "--coverage-report"],
            ["sdoc", "-f", "sdoc"],
            ["sdoc", "--fmt", "sdoc"],
            ["sdoc", "-i", "./common"],
            ["sdoc", "--include", "../common"],
            ["sdoc", "--markup", "tomdoc"]
        ]
        
        all_results = []
        for cmd in commands:
            result = subprocess.run(
                cmd,
                capture_output=True,
                text=True,
                timeout=30
            )
            all_results.append({
                "success": result.returncode == 0,
                "command": " ".join(cmd),
                "exit_code": result.returncode,
                "stdout": result.stdout,
                "stderr": result.stderr
            })
        
        return all_results[0]
    except Exception as e:
        return {
            "success": False,
            "command": "sdoc format options commands",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def sdoc_advanced_options(ctx: Context) -> dict:
    """
    测试 sdoc 的高级选项如可见性、编码、扩展等
    
    返回：
        dict: 包含执行结果的 JSON 对象
    """
    try:
        commands = [
            ["sdoc", "--visibility", "private"],
            ["sdoc", "-w", "10"],
            ["sdoc", "-U"],
            ["sdoc", "-E", "cgi=rb"],
            ["sdoc", "-a"],
            ["sdoc", "--all"],
            ["sdoc", "-e", "UTF-8"]
        ]
        
        all_results = []
        for cmd in commands:
            result = subprocess.run(
                cmd,
                capture_output=True,
                text=True,
                timeout=30
            )
            all_results.append({
                "success": result.returncode == 0,
                "command": " ".join(cmd),
                "exit_code": result.returncode,
                "stdout": result.stdout,
                "stderr": result.stderr
            })
        
        return all_results[0]
    except Exception as e:
        return {
            "success": False,
            "command": "sdoc advanced options commands",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def sdoc_github(ctx: Context) -> dict:
    """
    测试 sdoc 的 GitHub 集成功能
    
    返回：
        dict: 包含执行结果的 JSON 对象
    """
    try:
        github_url = "https://github.com/strictdoc-project/strictdoc.git"
        commands = [
            ["sdoc", "-g", github_url],
            ["sdoc", "--github", github_url]
        ]
        
        all_results = []
        for cmd in commands:
            result = subprocess.run(
                cmd,
                capture_output=True,
                text=True,
                timeout=60
            )
            all_results.append({
                "success": result.returncode == 0,
                "command": " ".join(cmd),
                "exit_code": result.returncode,
                "stdout": result.stdout,
                "stderr": result.stderr
            })
        
        return all_results[0]
    except Exception as e:
        return {
            "success": False,
            "command": "sdoc github commands",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()